/**
 * @author gao
 * @date 2025/11/1 15:48
 */

public enum TestEnum {
    BLACK(1, "黑色"), RED(2, "红色"), GREEN(3, "绿色");

    private int ordinal;
    private String color;

    TestEnum(int ordinal, String color) {
        this.ordinal = ordinal;
        this.color = color;
    }

    public static void main(String[] args) {
        TestEnum[] testEnums = TestEnum.values();
        for (int i = 0; i < testEnums.length; i++) {
            System.out.println(testEnums[i] + " " + testEnums[i].ordinal());
        }
        System.out.println("=====");
        try {
            // 通过valueOf方法将字符串"RED"转换为TestEnum枚举实例
            TestEnum testEnum = TestEnum.valueOf("RED");
            // 输出转换后的枚举实例
            System.out.println(testEnum);
        } catch (IllegalArgumentException e) {
            // 捕获并处理当传入的字符串与任何枚举常量名称不匹配时抛出的异常
            e.printStackTrace();
        }

        // 此方法实现的自然顺序是声明常量的顺序
        int to = RED.compareTo(BLACK);
        System.out.println(to);
    }
}